package com.xxl.job.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xxl.job.admin.core.model.XxlJobUser;
import com.xxl.job.admin.dao.XxlJobUserDao;
import com.xxl.job.admin.service.XxlJobUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import static com.xxl.job.admin.core.model.table.Tables.XXL_JOB_USER;

@Service
public class XxlJobUserServiceImpl extends ServiceImpl<XxlJobUserDao, XxlJobUser> implements XxlJobUserService {
    @Override
    public XxlJobUser loadByUserName(String username) {
        QueryWrapper q =QueryWrapper.create().where(XXL_JOB_USER.USERNAME.eq(username));
        return getOne(q);
    }

    @Override
    public Page<XxlJobUser> pageList(int start, int length, String username, int role) {
        QueryWrapper query=QueryWrapper.create()
                .where(XXL_JOB_USER.USERNAME.like(username).when(StringUtils.hasText(username)))
                .and(XXL_JOB_USER.ROLE.eq(role).when(role>1))
                .orderBy(XXL_JOB_USER.USERNAME.asc());
        Page<XxlJobUser> xxlJobUserPage = mapper.paginate(start,length,query);
        return xxlJobUserPage;
    }
}
